package beusoft.ccjk.com.apt.handler;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;

import beusoft.ccjk.com.apt.util.AnnotationUtil;
import beusoft.ccjk.com.apt.util.LogUtil;
import beusoft.ccjk.com.apt_anno.anno.AnnoField;
import beusoft.ccjk.com.apt_anno.anno.AnnoMethod;
import beusoft.ccjk.com.apt_anno.anno.AnnoParameter;
import beusoft.ccjk.com.apt_anno.anno.AnnoType;
import beusoft.ccjk.com.apt_anno.anno.AnnoVariable;

/**
 * Created by CCJK on 2018/11/24.
 */

public class TestHandler implements AnnotationHandler {
    private ProcessingEnvironment processingEnvironment;

    @Override
    public void attachProcessingEnv(ProcessingEnvironment processingEnvironment) {
        this.processingEnvironment=processingEnvironment;
    }

    @Override
    public Map<String, List<Element>> handleAnnotation(RoundEnvironment roundEnvironment) {
        Map<String, List<Element>> annotationMap = new HashMap<String, List<Element>>();
        Set<? extends Element> methods = roundEnvironment.getElementsAnnotatedWith(AnnoMethod.class);
        for (Element element : methods) {
            ExecutableElement method = (ExecutableElement) element;
            LogUtil.note(processingEnvironment,element, element.getClass().getCanonicalName());
            LogUtil.note(processingEnvironment,element, AnnotationUtil.getPackageName(this.processingEnvironment, element));

            TypeElement classElement = (TypeElement) element.getEnclosingElement();
            String fullClassName = classElement.getQualifiedName().toString();
            LogUtil.note(processingEnvironment,element, fullClassName);
        }
        //
        Set<? extends Element> parameters = roundEnvironment.getElementsAnnotatedWith(AnnoParameter.class);
        for (Element element : parameters) {
            VariableElement para = (VariableElement) element;
            LogUtil.note(processingEnvironment,element, element.getClass().getCanonicalName());
            LogUtil.note(processingEnvironment,element, AnnotationUtil.getPackageName(this.processingEnvironment, element));

            TypeElement classElement = (TypeElement) (element.getEnclosingElement().getEnclosingElement());
            String fullClassName = classElement.getQualifiedName().toString();
            LogUtil.note(processingEnvironment,element, fullClassName);
        }
        //
        Set<? extends Element> classes = roundEnvironment.getElementsAnnotatedWith(AnnoType.class);
        for (Element element : classes) {
            TypeElement cl = (TypeElement) element;
            LogUtil.note(processingEnvironment,element, element.getClass().getCanonicalName());
            LogUtil.note(processingEnvironment,element, AnnotationUtil.getPackageName(this.processingEnvironment, element));

//            TypeElement classElement = (TypeElement) element.getEnclosingElement();
            String fullClassName = cl.getQualifiedName().toString();
            LogUtil.note(processingEnvironment,element, fullClassName);
        }
        //
        Set<? extends Element> variables = roundEnvironment.getElementsAnnotatedWith(AnnoVariable.class);
        for (Element element : variables) {
            VariableElement variable = (VariableElement) element;
            LogUtil.note(processingEnvironment,element, "4 " + element.getClass().getCanonicalName());
            LogUtil.note(processingEnvironment,element, AnnotationUtil.getPackageName(this.processingEnvironment, element));

            TypeElement classElement = (TypeElement) element.getEnclosingElement();
            String fullClassName = classElement.getQualifiedName().toString();
            LogUtil.note(processingEnvironment,element, fullClassName);
        }

        Set<? extends Element> fields = roundEnvironment.getElementsAnnotatedWith(AnnoField.class);
        for (Element element : fields) {
            VariableElement variable = (VariableElement) element;
            LogUtil.note(processingEnvironment,element, "5 " + element.getClass().getCanonicalName());
            LogUtil.note(processingEnvironment,element, AnnotationUtil.getPackageName(this.processingEnvironment, element));

            TypeElement classElement = (TypeElement) element.getEnclosingElement();
            String fullClassName = classElement.getQualifiedName().toString();
            LogUtil.note(processingEnvironment,element, fullClassName);
        }


        return annotationMap;

    }
    /**
     * 获取某个字段所属的类的完整路径
     *
     * @param varElement 字段元素
     * @return
     */
    private String getParentClassName(VariableElement varElement) {
        // 获取该元素所在的类型,例如某个View是某个Activity的字段,这里就是获取这个Activity的类型
        TypeElement typeElement = (TypeElement) varElement.getEnclosingElement();
        // 获取typeElement的包名
        String packageName = AnnotationUtil.getPackageName(processingEnvironment, typeElement);
        // 类型的完整路径名,比如某个Activity的完整路径
        return packageName + "." + typeElement.getSimpleName().toString();
    }
}
